﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.BLL.GoodsDelivery;
using MINERP.MODEL.GoodsDelivery;

namespace ERPGoodsDelivery.Delivery
{
    public partial class FrmEmailSet : Form
    {
        //属性
        private static FrmEmailSet _instance;
        public static FrmEmailSet GetInstance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new FrmEmailSet();
                }
                return _instance;
            }
            set
            {
                _instance = value;
            }
        }

        private int currentShopId;
        public int CurrentShopId
        {
            get { return currentShopId; }
            set { currentShopId = value; }
        }
        //参数
        private DeliverFindBLL bll = new DeliverFindBLL();

        public FrmEmailSet()
        {
            InitializeComponent();
        }

        private void FrmEmailSet_Load(object sender, EventArgs e)
        {
            try
            {
                SetSendBoxInfo();
                BindShop();
                cbShopList.SelectedValue = currentShopId;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //保存信箱信息
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (txtPwd.Text != txtPwdConfirm.Text)
                {
                    MessageBox.Show("两次输入的密码不一致！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (cbNeedModigySendBox.Checked)
                {
                    //发信信箱
                    SaveSendBoxInfo();
                }
                //商户信箱
                SaveShopMailBox();
                MessageBox.Show("保存成功！", "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void cbNeedModigySendBox_CheckedChanged(object sender, EventArgs e)
        {
            SetEnableOrNotEnable();
        }
        //设置商户的邮箱
        private void cbShopList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                SetShopMail();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "确认", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        //--------------------公共方法--------------------
        private void BindShop()
        {
            try
            {
                List<ShopNormal> shops = bll.GetShopList();
                //排序
                shops = shops.OrderBy(p => p.ShopName).ToList();
                cbShopList.DisplayMember = "ShopName";
                cbShopList.ValueMember = "ShopId";
                cbShopList.DataSource = shops;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void SetEnableOrNotEnable()
        {
            if (cbNeedModigySendBox.Checked)
            {
                txtSmtpServer.Enabled = true;
                txtSendMailBox.Enabled = true;
                txtPwd.Enabled = true;
                txtPwdConfirm.Enabled = true;
                txtTitle.Enabled = true;
                txtContent.Enabled = true;
                textBoxUserName.Enabled = true;

            }
            else
            {
                txtSmtpServer.Enabled = false;
                txtSendMailBox.Enabled = false;
                txtPwd.Enabled = false;
                txtPwdConfirm.Enabled = false;
                txtTitle.Enabled = false;
                txtContent.Enabled = false;
                textBoxUserName.Enabled = false;
            }
        }

        /// <summary>
        /// 设置发信信箱
        /// </summary>
        private void SetSendBoxInfo()
        {
            try
            {
                txtSmtpServer.Text = Properties.Settings.Default.userSmtpServer;
                txtSendMailBox.Text = Properties.Settings.Default.userSendBoxAddress;
                txtPwd.Text = Properties.Settings.Default.userPwd;
                txtPwdConfirm.Text = Properties.Settings.Default.userPwd;
                txtTitle.Text = Properties.Settings.Default.userTitle;
                txtContent.Text = Properties.Settings.Default.userContent;
                textBoxUserName.Text=Properties.Settings.Default.UserName ;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 保存发信信箱
        /// </summary>
        private void SaveSendBoxInfo()
        {
            try
            {
                Properties.Settings.Default.userSmtpServer = txtSmtpServer.Text.Trim();
                Properties.Settings.Default.userSendBoxAddress = txtSendMailBox.Text;
                Properties.Settings.Default.userPwd = txtPwd.Text;
                Properties.Settings.Default.userTitle = txtTitle.Text;
                Properties.Settings.Default.userContent = txtContent.Text;
                Properties.Settings.Default.UserName = textBoxUserName.Text;
                Properties.Settings.Default.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 保存商户信箱
        /// </summary>
        private void SaveShopMailBox()
        {
            try
            {
                int shopId = Convert.ToInt32(cbShopList.SelectedValue);
                string mailAddr = txtReceiveAddress.Text.Trim();
                bll.UpdateShopMail(shopId, mailAddr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #region 商户邮箱
        /// <summary>
        /// 商户邮箱
        /// </summary>
        private void SetShopMail()
        {
            try
            {
                int shopId = Convert.ToInt32(cbShopList.SelectedValue);
                string mailAddr = bll.GetShopMailAddr(shopId);
                txtReceiveAddress.Text = mailAddr;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion 
    }
}
